/*------------------------------------------------------------------------------------------------\
| Horrible X-Wing game by Steven Haber
| st_haber@hotmail.com
|
| menu.cpp: contains the class Menu, which displays a menu depending on the state of the program
\------------------------------------------------------------------------------------------------*/

#include "Menu.h"


//Constructor
Menu::Menu(sf::Sprite *sprites[], sf::RenderWindow *out, Sound *sounds)
{
	mOut = out;
	mBackground = *sprites[MENU];
	mCursor = *sprites[XWING];
	mSounds = sounds;

	mCursor.SetRotationCenter(mCursor.GetWidth()/2, mCursor.GetHeight()/2);
	mCursor.SetRotation((float)270);
	mCursor.SetPosition((float)SCREENX/2 - CURSORSPACINGX, (float)SCREENY/2 - mCursor.GetHeight());

	//Settings texts
	mTextTitle.SetText("x-wing");
	mTextStartup1.SetText("one player");
	mTextStartup2.SetText("two players");
	mTextIngame1.SetText("resume");
	mTextIngame2.SetText("exit");
	mTextEndgame1.SetText("new game");
	mTextEndgame2.SetText("exit");

	//Goldish color
	sf::Color textColor(200, 225, 0, 255);
	
	//Finalizing texts
	mTextTitle.SetFont("font.ttf");
	mTextTitle.SetSize(80);
	mTextTitle.SetColor(textColor);

	mTextStartup1.SetFont("font.ttf");
	mTextStartup1.SetSize(50);
	mTextStartup1.SetColor(textColor);
	
	mTextStartup2.SetFont("font.ttf");
	mTextStartup2.SetSize(50);
	mTextStartup2.SetColor(textColor);
	
	mTextIngame1.SetFont("font.ttf");
	mTextIngame1.SetSize(50);
	mTextIngame1.SetColor(textColor);

	mTextIngame2.SetFont("font.ttf");
	mTextIngame2.SetSize(50);
	mTextIngame2.SetColor(textColor);

	mTextEndgame1.SetFont("font.ttf");
	mTextEndgame1.SetSize(50);
	mTextEndgame1.SetColor(textColor);

	mTextEndgame2.SetFont("font.ttf");
	mTextEndgame2.SetSize(50);
	mTextEndgame2.SetColor(textColor);

	//Positioning text
	sf::Rect<float> *rect;
	rect = new sf::Rect<float>(mTextTitle.GetRect());
	mTextTitle.SetPosition(SCREENX/2 - rect->GetWidth()/2, 60);
	delete rect;

	rect = new sf::Rect<float>(mTextStartup1.GetRect());
	mTextStartup1.SetPosition(SCREENX/2 - rect->GetWidth()/2, (float)SCREENY/2 - mCursor.GetHeight());
	delete rect;

	rect = new sf::Rect<float>(mTextStartup2.GetRect());
	mTextStartup2.SetPosition(SCREENX/2 - rect->GetWidth()/2, (float)SCREENY/2);
	delete rect;

	rect = new sf::Rect<float>(mTextIngame1.GetRect());
	mTextIngame1.SetPosition(SCREENX/2 - rect->GetWidth()/2, (float)SCREENY/2 - mCursor.GetHeight());
	delete rect;

	rect = new sf::Rect<float>(mTextIngame2.GetRect());
	mTextIngame2.SetPosition(SCREENX/2 - rect->GetWidth()/2, (float)SCREENY/2);
	delete rect;

	rect = new sf::Rect<float>(mTextEndgame1.GetRect());
	mTextEndgame1.SetPosition(SCREENX/2 - rect->GetWidth()/2, (float)SCREENY/2 - mCursor.GetHeight());
	delete rect;

	rect = new sf::Rect<float>(mTextEndgame2.GetRect());
	mTextEndgame2.SetPosition(SCREENX/2 - rect->GetWidth()/2, (float)SCREENY/2);
	delete rect;
}


//Shows the menu over whatever else is going on
int Menu::showMenu(int whichMenu)
{
	bool exitMenu = false;
	
	//Startup menu needs the bright picture first
	if(whichMenu == STARTUP)
	{
		mTimer.Reset();
		while(mTimer.GetElapsedTime() < STARTUPMENUTIME)
		{
			sf::Event event;
			while(mOut->GetEvent(event))
			{
				if(event.Type == sf::Event::Closed)
					exit(EXIT_SUCCESS);
			}
			
			mOut->Draw(mBackground);
			mOut->Draw(mTextTitle);
			mOut->Display();
		}
	}

	sf::Color darkBackground(100, 100, 100, 225);
	mBackground.SetColor(darkBackground);

	while(!exitMenu)
	{
		sf::Event event;

		while(mOut->GetEvent(event))
		{
			if(event.Type == sf::Event::Closed)
				exit(EXIT_SUCCESS);
			if(event.Type == sf::Event::KeyPressed)
			{	
				//These move the cursor back and forth
				if(event.Key.Code == sf::Key::Down && mCursor.GetTop() == (float)SCREENY/2 - mCursor.GetHeight())
				{
					mCursor.SetTop((float)SCREENY/2);
					mSounds->playSound(SELECT);
				}

				if(event.Key.Code == sf::Key::Up && mCursor.GetTop() == (float)SCREENY/2)
				{
					mCursor.SetTop((float)SCREENY/2 - mCursor.GetHeight());
					mSounds->playSound(SELECT);
				}
				
				//Startup menu case
				if(whichMenu == STARTUP)
				{
					if(event.Key.Code == sf::Key::Return)
					{
						mSounds->playSound(ENTER);

						//Returning number of players here
						if(mCursor.GetTop() == (float)SCREENY/2 - mCursor.GetHeight())
							return 1;
						else
						{
							mCursor.SetTop((float)SCREENY/2 - mCursor.GetHeight());
							return 2;
						}
					}
				}

				//Ingame menu case
				else if(whichMenu == INGAME)
				{
					if(event.Key.Code == sf::Key::Return)
					{
						mSounds->playSound(ENTER);

						if(mCursor.GetTop() == (float)SCREENY/2 - mCursor.GetHeight())
							exitMenu = true;
						else
							exit(EXIT_SUCCESS);
					}
				}

				//Endgame menu case
				else if(whichMenu == ENDGAME)
				{
					if(event.Key.Code == sf::Key::Return)
					{
						mSounds->playSound(ENTER);

						if(mCursor.GetTop() == (float)SCREENY/2 - mCursor.GetHeight());
						else
							exit(EXIT_SUCCESS);
					}
				}
			}
		}
		
		mOut->Draw(mBackground);
		mOut->Draw(mCursor);
		mOut->Draw(mTextTitle);

		//Cases for the specific menus
		if(whichMenu == STARTUP)
		{
			mOut->Draw(mTextStartup1);
			mOut->Draw(mTextStartup2);
		}
		
		else if(whichMenu == INGAME)
		{
			mOut->Draw(mTextIngame1);
			mOut->Draw(mTextIngame2);
		}

		else if(whichMenu == ENDGAME)
		{
			mOut->Draw(mTextEndgame1);
			mOut->Draw(mTextEndgame2);
		}
		
		mOut->Display();
	}

	return 0;
}